import axios from 'axios'
import { getToken } from '@/utils/auth'

const request = axios.create({
  baseURL: '/api', timeout: 5000
})

request.interceptors.request.use(config => {
  const token = getToken()
  if (token) {
    config.headers.token = `${token}`
  }
  return config
}, error => {
  return Promise.reject(error)
})

request.interceptors.response.use(response => {
  if (response.data.code !== 1) {
    return Promise.reject(new Error(response.data.message))
  }
  return response.data
}, error => {
  return Promise.reject(error)
})

export default request
